<?php
namespace app\common\model;

use app\admin\model\State;
use app\admin\model\Stores;
use app\common\traits\Gate;
use app\common\traits\Text;
use think\Db;
use think\Model;

class Order_error extends Model
{
    use Text;
    protected $type = [
        'yctime'=>'timestamp:Y-m-d',//预产期
        'sjtime'=>'timestamp:Y-m-d',//收集日期
        'yypstime'=>'timestamp:Y-m-d',//预约拍摄时间
        'qxpstime'=>'timestamp:Y-m-d',//取消拍摄时间
        'wdpstime'=>'timestamp:Y-m-d',//未到拍摄时间
        'zztime'=>'timestamp:Y-m-d',//追踪时间
        'jdtime'=>'timestamp:Y-m-d G:i',//建单时间
        'cztime'=>'timestamp:Y-m-d',//最后操作时间
    ];


    /**  条件筛选
     * @param $data   表单提交的数据
     * @return mixed  where条件
     */
    public  function map($data){
        //ID
        if(!empty($data['id'])){
            trim($data['id']);
            $map['id']=['eq',$data['id']];
        }
        //顾客姓名
        if(!empty($data['name'])){
            trim($data['name']);
            $map['name']=['eq',$data['name']];
        }
        //妈妈手机
        if(!empty($data['maphone'])){
            trim($data['maphone']);
            $map['maphone']=['eq',$data['maphone']];
        }
        //家人手机
        if(!empty($data['otherphone'])){
            trim($data['otherphone']);
            $map['otherphone']=['eq',$data['otherphone']];
        }
        //微信
        if(!empty($data['weixin'])){
            trim($data['weixin']);
            $map['weixin']=['eq',$data['weixin']];
        }
        //QQ
        if(!empty($data['qq'])){
            trim($data['qq']);
            $map['qq']=['eq',$data['qq']];
        }
        //网络部渠道来源
        if(!empty($data['qudao'])){
            $map['qudao']=['eq',$data['qudao']];
        }
        //照片分类
        if(!empty($data['fenlei'])){
            $map['fenlei']=['eq',$data['fenlei']];
        }
        //区域
        if(!empty($data['area'])){
            $map['area']=['eq',$data['area']];
        }
        //收集医院
        if(!empty($data['yiyuan'])){
            $map['yiyuan']=['eq',$data['yiyuan']];
        }
        //收集日期
        if(!empty($data['sjtime']) ){
            $sjtime = time_StrToArr($data['sjtime']);
            $map['sjtime']=['between time',$sjtime];
        }
        //预产期
        if(!empty($data['yctime'])){
            $yctime = time_StrToArr($data['yctime']);
            $map['yctime']=['between time',$yctime];
        }
        //建单客服
        if(!empty($data['jdry'])){
            $map['jdry']=['eq',$data['jdry']];
        }
        //建单时间
        if(!empty($data['jdtime']) ){
            $jdtime = time_StrToArr($data['jdtime']);
            $map['jdtime']=['between time',$jdtime];
        }
        //建单部门
        if(!empty($data['qd_depar']) ){
            $map['qd_depar']=['eq',$data['qd_depar']];
        }
        //邀约客服
        if(!empty($data['yykf'])){
            $map['yykf']=['eq',$data['yykf']];
        }
        //预约场馆
        if(!empty($data['yycg'])){
            $map['yycg']=['eq',$data['yycg']];
        }
        //业务状态
        if(!empty($data['ywstate'])){
            $map['ywstate']=['eq',$data['ywstate']];
        }
        //追踪日期
        if(!empty($data['zztime']) ){
            $zztime = time_StrToArr($data['zztime']);
            $map['zztime']=['between time',$zztime];
        }
        //预约拍摄时间
        if(!empty($data['yypstime']) ){
            $yypstime = time_StrToArr($data['yypstime']);
            $map['yypstime']=['between time',$yypstime];
        }
        //取消拍摄时间
        if(!empty($data['qxpstime'])){
            $qxpstime = time_StrToArr($data['qxpstime']);
            $map['qxpstime']=['between time',$qxpstime];
        }
        //未到拍摄时间
        if(!empty($data['wdpstime']) ){
            $wdpstime = time_StrToArr($data['wdpstime']);
            $map['wdpstime']=['between time',$wdpstime];
        }
        //金牌匹配
        if(!empty($data['jinpai'])){
            $map['jinpai']=['eq',$data['jinpai']];
        }
        //会所地址
        if(!empty($data['hsdzid'])){
            $map['hsdzid']=['eq',$data['hsdzid']];
        }
        //会所组长
        if(!empty($data['hszzid'])){
            $map['hszzid']=['eq',$data['hszzid']];
        }
        //会所专员
        if(!empty($data['hszyid'])){
            $map['hszyid']=['eq',$data['hszyid']];
        }
        //会所状态
        if(!empty($data['hdstate'])){
            $map['hdstate']=['eq',$data['hdstate']];
        }
        //课程
        if(!empty($data['kcid'])){
            $map['kcid']=['eq',$data['kcid']];
        }
        return $map;
    }



    /**
     * 组合条件筛选和角色筛选
     * @param array $map
     * @return array|mixed
     */
    public function maps($map){
        //$order = new Order();
        $data = $this->map($map);
        $mapjuese = $this->mapJueSe();
        if(empty($data)){
            $datas = $mapjuese;
        }elseif(empty($mapjuese)){
            $datas = $data;
        }else{
            $datas = array_merge($mapjuese,$data);
        }
        return $datas;
    }




    /*  新增或修改订单  */
    public function edit($data,$khd){
        $order_log = new Order_log;
        //新建
        if(empty($data['id'])){
            $res = $this->save($data);
            //成功，更新日志
            if($res){
                $content = "新增订单$this->id";
                $data['id'] = $this->id;
                $logdata = $order_log->getlogdata($data,1,$khd,$content);
                $ret = $order_log->log($logdata);//日志
                return ['status'=>'1','info'=>"新增成功",'url'=>url('olist')];
            }else{//失败
                return ['status'=>'0','info'=>$this->getError()];
            }
        }else{//更新
            $olddata =  $this->where(['id'=>$data['id']])->find()->toArray();
            $res = $this->allowField(true)->isUpdate()->save($data);
            //成功，更新日志
            if($res){
                $content = getlogdetail($olddata,$data);
                $logdata = $order_log->getlogdata($data,2,$khd,$content);
                $ret = $order_log->log($logdata);//日志
                //分页
                $session_p = session('map');
                if(empty($session_p['p'])){
                    return ['status'=>'1','info'=>'数据更新成功！','url'=>url('olist',['editid'=>$data['id']])];
                }else{
                    return ['status'=>'1','info'=>'数据更新成功！','url'=>url('olist',['editid'=>$data['id'],'p'=>$session_p['p']])];
                }
            }else{//失败
                return ['status'=>'0','info'=>$this->getError()];
            }
        }
    }



    public function getEditInfo($state,$id,$user=''){
        $ywstates = $this->checkState(); //能够编辑的状态组
        $nowuser = session('admin_user.adminid');
        if(empty($user)){
            if( in_array($state,array_keys($ywstates)) ){
                return ['msg'=>"恭喜您有权限访问！",'status'=>"1","url"=>url('edit',['id'=>$id])];
            }else{
                return ['msg'=>"对不起，您没有权限修改！",'status'=>"0",'icon'=>4];
            }
        }else{
            if( in_array($state,array_keys($ywstates)) && $nowuser == $user ){
                return ['msg'=>"恭喜您有权限访问！",'status'=>"1","url"=>url('edit',['id'=>$id])];
            }else{
                return ['msg'=>"对不起，您没有权限修改！",'status'=>"0",'icon'=>4];
            }
        }

    }


}

?>